package models

import (
	"fmt"
	"time"

	"gorm.io/gorm/clause"
)

var Models []interface{}

type DateTime time.Time

func (t DateTime) MarshalDateTime() ([]byte, error) {
	tTime := time.Time(t)
	return []byte(fmt.Sprintf("%v", tTime.Format("2006-01-02 15:04:05"))), nil
}

func (t DateTime) MarshalDate() ([]byte, error) {
	tTime := time.Time(t)
	return []byte(fmt.Sprintf("%v", tTime.Format("2006-01-02"))), nil
}

// db.Clauses(models.Paginate(10, 10))
func Paginate(page, limit int) clause.Expression {
	if limit <= 0 {
		limit = 20
	}

	if page <= 0 {
		page = 1
	}
	offset := (page - 1) * limit
	return clause.Limit{Limit: &limit, Offset: offset}
}
